package com.shire.shire_interface.service.keyword;

import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.constants.Constants;
import com.shire.shire_interface.dao.keyword.WxKeywordReplyDao;
import com.shire.shire_interface.dao.keyword.WxReplyRecordDao;
import com.shire.shire_interface.model.keyword.WxKeywordReply;
import com.shire.shire_interface.model.keyword.WxKeywordReplyExample;
import com.shire.shire_interface.model.keyword.WxReplyRecord;
import com.shire.shire_interface.model.keyword.WxReplyRecordExample;
import com.shire.shire_interface.model.user.WxAccount;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class WxKeywordReplyService {

    @Resource
    private WxKeywordReplyDao wxKeywordReplyDao;

    @Resource
    private WxReplyRecordDao replyRecordDao;

    public int insert(WxKeywordReply WxKeywordReply) {
        return wxKeywordReplyDao.insertSelective(WxKeywordReply);
    }

    public int update(WxKeywordReply wxKeywordReply){
        return wxKeywordReplyDao.updateByPrimaryKeySelective(wxKeywordReply);
    }

    public List<WxKeywordReply> selectByExample(WxKeywordReplyExample wxKeywordReplyExample) {
        return wxKeywordReplyDao.selectByExample(wxKeywordReplyExample);
    }

    public int deleteByPrimaryKey(Integer taskId) {
        //假删除
        WxKeywordReply reply = wxKeywordReplyDao.selectByPrimaryKey(taskId);
        reply.setStatus(1);
        reply.setUpdateTime(new Date());
        reply.setStatus(1);


        WxReplyRecord replyRecord = new WxReplyRecord();
        replyRecord.setStatus(1);
        WxReplyRecordExample example = new WxReplyRecordExample();
        example.createCriteria().andReplyruleidEqualTo(taskId);
        replyRecordDao.updateByExampleSelective(replyRecord,example);

        return wxKeywordReplyDao.updateByPrimaryKey(reply);
    }

    public List<WxKeywordReply> getReplayContent(String groupNo, String content, WxAccount account){
        WxReplyRecordExample replyRecordExample = new WxReplyRecordExample();
        replyRecordExample.createCriteria().andGroupaccountEqualTo(groupNo).andStatusEqualTo(0);
        List<WxReplyRecord> replyRecords = replyRecordDao.selectByExample(replyRecordExample);

        if (replyRecords == null || replyRecords.size() < 1)
            return null;
        List<WxKeywordReply> replyList = new ArrayList<>();
        for (WxReplyRecord record : replyRecords){
            WxKeywordReplyExample example = new WxKeywordReplyExample();
            example.createCriteria().andWxaccountEqualTo(account.getWxid()).andIdEqualTo(record.getReplyruleid()).andStatusEqualTo(Constants.YesNo.YES.getCode());

            WxKeywordReply reply = null;

            List<WxKeywordReply> list = wxKeywordReplyDao.selectByExample(example);
            if (list.size() > 0)
                reply = list.get(0);
            if (reply == null)
                continue;

            List<String> keywords = (List<String>) Arrays.asList(reply.getKeyword().split(","));
            filterKeyWord(content, replyList, reply, keywords);
            if (replyList.size()>0){//命重一条规则就退出
                break;
            }
        }
        return replyList;
    }

    public List<WxKeywordReply>  getFriendReplayContent(String content, WxAccount account){
        WxKeywordReplyExample example = new WxKeywordReplyExample();
        example.createCriteria().andWxaccountEqualTo(account.getWxid()).andTypeEqualTo(1).andStatusEqualTo(0);

        List<WxKeywordReply> retList = new ArrayList<>();

        List<WxKeywordReply> replyList = wxKeywordReplyDao.selectByExample(example);
        for (WxKeywordReply reply : replyList) {
            List<String> keywords = (List<String>) Arrays.asList(reply.getKeyword().split(","));
            filterKeyWord(content, retList, reply, keywords);
            if (retList.size()>0){//命重一条规则就退出
                break;
            }
        }

        return retList;
    }

    private void filterKeyWord(String content, List<WxKeywordReply> retList, WxKeywordReply reply, List<String> keywords) {
        for (String keyword : keywords) {
            JSONObject contentObj = JSONObject.parseObject(content);
            String strContent = contentObj.getString("content");
            if (StringUtils.isNotBlank(keyword) && strContent.contains(keyword)){
                retList.add(reply);
            }
        }
    }


    public void updateReplyRecodAfterSend(String groupNo, Integer csId, Integer replyId){
        WxReplyRecordExample replyRecordExample = new WxReplyRecordExample();
        replyRecordExample.createCriteria().andCsidEqualTo(csId).andGroupaccountEqualTo(groupNo).andReplyruleidEqualTo(replyId).andStatusEqualTo(0);

        List<WxReplyRecord> replyRecords = replyRecordDao.selectByExample(replyRecordExample);
        if (replyRecords == null || replyRecords.size() < 1)
            return ;

        WxReplyRecord record = replyRecords.get(0);
        if (record.getReplycount() == null)
            record.setReplycount(1);
        else {
            record.setReplycount(record.getReplycount() + 1);
        }
        record.setUpdatetime(new Date());
        replyRecordDao.updateByPrimaryKey(record);
    }

    public void saveFriendReplyRecord(String friendWxid, WxAccount account, Integer replyId){
        WxReplyRecordExample replyRecordExample = new WxReplyRecordExample();
        replyRecordExample.createCriteria().andReplyruleidEqualTo(replyId).andGroupaccountEqualTo(friendWxid);
        List<WxReplyRecord> recordList = replyRecordDao.selectByExample(replyRecordExample);
        WxReplyRecord record = null;
        if (recordList.size()>0){
            record = recordList.get(0);
            record.setReplycount(record.getReplycount() + 1);
        }else {
            record = new WxReplyRecord();
            record.setReplycount(1);
            record.setCsid(account.getCsid());
            record.setWxaccountid(account.getWxid());
            record.setGroupaccount(friendWxid);
            record.setReplyruleid(replyId);
            record.setStatus(0);
        }

        if (record.getId() == null){
            record.setCreatetime(new Date());
            replyRecordDao.insertSelective(record);
        }else {
            record.setUpdatetime(new Date());
            replyRecordDao.updateByPrimaryKeySelective(record);
        }
    }
}
